<template>
  <el-page-header @back="goBack" icon="">
    <template #title>
      <span class="page-title">汕头大学</span>
    </template>
    <template #content>
      <span class="text-large font-600 mr-3">脑力奥运刷题系统</span>
    </template>
    <template #extra>
      <div class="flex items-center">
        <el-button
            @click="handleAuth"
            link
            class="auth-button"
            :type="isLoggedIn ? 'danger' : 'primary'"
        >
          {{ isLoggedIn ? '退出' : '登录' }}
        </el-button>
      </div>
    </template>
  </el-page-header>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import router from "../router";

const isLoggedIn = ref(false); // 跟踪是否登录

// 页面加载时检查是否有 token，初始化登录状态
onMounted(() => {
  if (localStorage.getItem('token')) {
    isLoggedIn.value = true;
  }
});

const goBack = () => {
  console.log('go back');
};

// 登录/退出操作
const handleAuth = () => {
  if (isLoggedIn.value) {
    // 退出操作
    localStorage.removeItem('token');
    isLoggedIn.value = false;
    console.log('已退出登录');
    router.push('/login'); // 退出后跳转到登录页
  } else {
    // 登录操作
    router.push('/login'); // 登录跳转到登录页
  }
};
</script>

<style scoped>
/* 页面头部标题样式 */
.page-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
}

/* 自定义按钮样式 */
.auth-button {
  font-size: 16px;
  padding: 8px 20px;
  border-radius: 25px;
  transition: all 0.3s ease;
}

.auth-button:hover {
  background-color: #ffd04b; /* 悬停时的黄色背景色 */
  color: #fff;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* 增加悬停阴影效果 */
}

.el-page-header {
  padding: 15px 30px;
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  margin-bottom: 20px;
}

.el-page-header .el-button {
  transition: background-color 0.3s ease;
}

/* 调整页面顶部的内容布局，使其更加对齐 */
.el-page-header .extra {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}

/* 页面内容的自定义字体 */
.text-large {
  font-size: 20px;
  font-weight: 600;
  color: #555;
}
</style>
